<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class admincp extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library("soap");
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->load->helper(array('form', 'url'));
		@session_start();
	}
 
	public function index($nof =null)
	{
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			if($this->checkrole($this->session->userdata('adminnam')) == null || $this->checkrole($this->session->userdata('adminnam')) == "3" || $this->checkrole($this->session->userdata('adminnam')) == "4" ){
				$this->load->view("user/pending");
			}else{

				$this->load->model("transms_model");
				$this->load->model("report_model");
				//Hien thi MO
				if (isset($_REQUEST['searchbtt'])) {
					$content = $this->input->post('contentsearch', true);
					$type=$this->input->post('typesearch', true);

					if($content==null || $type==null){
						$data['shoutbox'] =3;
						$data['totalsms'] = 0;
						$data['receivesmo'] = array();
						$data['pages'] = array();
					}else{
						$config['base_url'] = site_url('admincp/index');
						$config['total_rows'] = $this->transms_model->totalmo($content,$type);
						$config['per_page'] = 50;
						$config['prev_link'] = 'Đầu';
						$config['next_link'] = 'Tiếp';
						$this->pagination->initialize($config);
						$data['receivesmo'] = $this->transms_model->show_mobytype($type,$content,$config['per_page'],$this->uri->segment(3));
						$data['pages'] = $this->pagination->create_links();
						$data['shoutbox'] = 0;
						$data['totalsms'] = $this->transms_model->totalmo($content,$type);
					}

				}else{
					date_default_timezone_set('Asia/Bangkok');
					$config['base_url'] = site_url('admincp/index');
					$config['total_rows'] = $this->transms_model->totalmotoday(date('d-m-Y'));
					$config['per_page'] = 50;
					$config['prev_link'] = 'Đầu';
					$config['next_link'] = 'Tiếp';
					$this->pagination->initialize($config);
					$data['receivesmo'] = $this->transms_model->show_mo($config['per_page'],$this->uri->segment(3));
					$data['pages'] = $this->pagination->create_links();
					$data['shoutbox'] = 0;
					$data['totalsms'] = $this->transms_model->totalmo();
					$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
						
				}
				/*  ---- - - -- --REPORT - - - - - -   */
				$date = date('d-m-Y');
				$this->report_model->removeallreportday();
				for ($i = 0; $i <= 8; $i++) {
					$new_date = strtotime ( '-'.$i.' day' , strtotime ( $date ) ) ;
					$new_date = date ( 'd-m-Y' , $new_date );
					$this->report_model->insert_report_day($this->report_model->report_for_week($new_date),$new_date);
				}

				$data['reportbydays'] = $this->report_model->get_report_day();
				$this->load->model('chatbox_model');
				$config['base_url'] = site_url('webhome/');
				$config['total_rows'] = $this->chatbox_model->_totalchatbox();
				$config['per_page'] = 30;
				$data['chatbox'] = $this->chatbox_model->_showchatbox($config['per_page'],$this->uri->segment(2));
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->model('user_model');
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$data['shoutbox'] =0;
				//pagination
				$config['base_url'] = site_url('admincp/usercp');
				$config['total_rows'] = $this->user_model->totaluser();
				$config['per_page'] = 30;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['listusers'] = $this->user_model->_listuser($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				//end paging
				$data['totaluser'] = $this->user_model->totaluser();
				$data['total_member'] = $this->user_model->total_member();
				$data["total_user_list"] = $this->user_model->showalluser();
				$this->load->model('log_model');
				$config['base_url'] = site_url('admincp/index');
				$config['total_rows'] = $this->log_model->total_log();
				$config['per_page'] = 15;
				$config['prev_link'] = 'First';
				$config['next_link'] = 'Next';
				$this->pagination->initialize($config);
				$data["total_log"] = $this->log_model->show_log($config['per_page'],$this->uri->segment(3));
				$data['pages_logs'] = $this->pagination->create_links();
				$data["pageviews"] = $this->log_model->total_log();
				$data['user_active'] = $this->user_model->totaluseractive();
				$data['user_unactive']=$this->user_model->totaluserunactive();
				$this->load->model("content_model");
				$data["totalapp"] = $this->content_model->totalappactive();
				$data["totalappunactive"] = $this->content_model->totalappunactive();

				$this->load->view('appstore/admin/dashboard',$data);
			}
		}
	}


	public function member(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{ 
				//--------------Addition Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['reportbydays'] = $this->report_model->get_report_day();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------Addition Function ----------------------------------
					
				$config['base_url'] = site_url('admincp/member');
				$config['total_rows'] = $this->user_model->total_member();
				$config['per_page'] = 50;
				$config['prev_link'] = 'Last';
				$config['next_link'] = 'Next';
				$this->pagination->initialize($config);
				$data['members'] = $this->user_model->_listmember($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$data['shoutbox'] = 0;
				$data['total_member'] = $this->user_model->total_member();
				$this->load->view('appstore/admin/dashboard',$data);
			
		}
	}
	public function checkmoservices($nof =null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			
				//--------------Addition Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['reportbydays'] = $this->report_model->get_report_day();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------Addition Function ----------------------------------

				if (isset($_REQUEST['searchbtt'])) {
					$content = $this->input->post('contentsearch', true);
					$type=$this->input->post('typesearch', true);
					if($content==null || $type=="none"){
						redirect('admincp/checkmoservices');
					}
					if($content==null || $type==null){
						$data['shoutbox'] =3;
						$data['totalsms'] = 0;
						$data['receivesmo'] = array();
						$data['pages'] = array();
					}else{
						$config['base_url'] = site_url('admincp/index');
						$config['total_rows'] = $this->transms_model->totalmo($content,$type);
						$config['per_page'] = 50;
						$config['prev_link'] = 'Đầu';
						$config['next_link'] = 'Tiếp';
						$this->pagination->initialize($config);
						$data['receivesmo'] = $this->transms_model->show_mobytype($type,$content,$config['per_page'],$this->uri->segment(3));
						$data['pages'] = $this->pagination->create_links();
						$data['shoutbox'] = 0;
						$data['totalsms'] = $this->transms_model->totalmo($content,$type);


						$data['adminname'] = $this->session->userdata('adminnam');
						$data['adminid'] = $this->session->userdata('adminid');
						$this->load->model('report_model');
						$data['reportbydays'] = $this->report_model->get_report_day();
						$this->load->model('chatbox_model');
						$config['base_url'] = site_url('webhome/');
						$data['chatbox'] = $this->chatbox_model->_showchatbox($config['per_page'],$this->uri->segment(2));
						$data['adminname'] = $this->session->userdata('adminnam');
						$data['adminid'] = $this->session->userdata('adminid');
						$this->load->model('user_model');
						$data['adminname'] = $this->session->userdata('adminnam');
						$data['adminid'] = $this->session->userdata('adminid');
						$config['base_url'] = site_url('admincp/usercp');
						$config['prev_link'] = 'Đầu';
						$config['next_link'] = 'Tiếp';
						$this->pagination->initialize($config);
						$data['listusers'] = $this->user_model->_listuser($config['per_page'],$this->uri->segment(3));
						$data['totaluser'] = $this->user_model->totaluser();
						$this->load->model('transms_model');
						$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
					}

				}else{
					date_default_timezone_set('Asia/Bangkok');
					$config['base_url'] = site_url('admincp/checkmoservices');
					$config['total_rows'] = $this->transms_model->totalmo();
					$config['per_page'] = 50;
					$config['prev_link'] = 'Đầu';
					$config['next_link'] = 'Tiếp';
					$this->pagination->initialize($config);
					$data['receivesmo'] = $this->transms_model->show_mo_sum($config['per_page'],$this->uri->segment(3));
					$data['pages'] = $this->pagination->create_links();
				}

				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function fmenu(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			//--------------Addition Function ----------------------------------
			$this->load->model("transms_model");
			$this->load->model('report_model');
			$this->load->model('chatbox_model');
			$this->load->model('user_model');
			$this->load->model('transms_model');
			//$data['reportbydays'] = $this->report_model->get_report_day();
			$data['totaluser'] = $this->user_model->totaluser();
			$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
			$data['totalsms'] = $this->transms_model->totalmo();
			//--------------Addition Function ----------------------------------

			$this->load->model('menu_model');
			$data['rootmenu'] = $this->menu_model->showallmenu();
			if (isset($_REQUEST['submit'])) {
				$menuname   = $this->input->post('menuname', true);
				$menutext   = $this->input->post('menutext', true);
				$menuroot   = $this->input->post('rootmenu', true);
				$menuactive =  $this->input->post('active',true);
				$menuorder = $this->input->post('menuorder',true);
				$this->menu_model->_addmenu($menuroot,$menuname,$menutext,$menuorder,$menuactive);
				redirect('admincp/fmenu');
			}
			$data['listmenu'] = $this->menu_model->showmenubyorder(2);
			$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function edit_menu($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}
		else{
			//--------------BEGIN Function ----------------------------------
			$this->load->model("transms_model");
			$this->load->model('report_model');
			$this->load->model('chatbox_model');
			$this->load->model('user_model');
			$this->load->model('transms_model');
			$data['totaluser'] = $this->user_model->totaluser();
			$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
			$data['totalsms'] = $this->transms_model->totalmo();
			//--------------END Function ----------------------------------
			$this->load->model('menu_model');
			$data['rootmenu'] = $this->menu_model->showallmenu();
			if (isset($_REQUEST['submit'])) {
				$menuname   = $this->input->post('menuname', true);
				$menutext   = $this->input->post('menutext', true);
				$menuroot   = $this->input->post('rootmenu', true);
				$menuactive =  $this->input->post('active',true);
				$menuorder = $this->input->post('menuorder',true);
				$this->menu_model->_edit_menu($id,$menuroot,$menuname,$menutext,$menuorder,$menuactive);
				redirect('admincp/fmenu');
			}
			$data['listmenu'] = $this->menu_model->getdetailmenu($id);
			$this->load->view('appstore/admin/dashboard',$data);
		}

	}

	public function deletemenu($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('menu_model');
			$this->menu_model->deletemenu($id);
			redirect('admincp/fmenu');
		}
	}
	public function searchadmin($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			//--------------BEGIN Function ----------------------------------
			$this->load->model("transms_model");
			$this->load->model('report_model');
			$this->load->model('chatbox_model');
			$this->load->model('user_model');
			$this->load->model('transms_model');
			$data['totaluser'] = $this->user_model->totaluser();
			$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
			$data['totalsms'] = $this->transms_model->totalmo();
			//--------------END Function ----------------------------------
			$this->load->model('admin_model');
			if (isset($_REQUEST['bttSearch'])) {
				$username = $this->input->post('username',true);
				$data['alladmin'] = $this->admin_model->searchuseradmin($username);
			}

			$this->load->view('appstore/admin/dashboard',$data);
		}
	}
	public function adminuser(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			//--------------BEGIN Function ----------------------------------
			$this->load->model("transms_model");
			$this->load->model('report_model');
			$this->load->model('chatbox_model');
			$this->load->model('user_model');
			$this->load->model('transms_model');
			$data['totaluser'] = $this->user_model->totaluser();
			$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
			$data['totalsms'] = $this->transms_model->totalmo();
			//--------------END Function ----------------------------------
			$this->load->model('admin_model');
			$config['base_url'] = site_url('admincp/adminuser');
			$config['total_rows'] = $this->admin_model->total_admin();
			$config['per_page'] = 10;
			$config['prev_link'] = 'Đầu';
			$config['next_link'] = 'Tiếp';
			$this->pagination->initialize($config);
			$data['alladmin'] = $this->admin_model->show_all_admin($config['per_page'],$this->uri->segment(3));
			$data['pages'] = $this->pagination->create_links();

			$this->load->view('appstore/admin/dashboard',$data);
		}
	}



	public function checkrole($username){
		$this->load->model('admin_model');
		return $this->admin_model->check_admin_user($username);
			
	}
	public function add_useradmin(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('admin_model');
				if (isset($_REQUEST['submit'])) {
					$level = $this->input->post('adminpage',true);
					$pass = $this->input->post('adminpass',true);
					$username = $this->input->post('adminname',true);
					$this->admin_model->add_useradmin($username,md5($pass),$level);
					redirect('admincp/adminuser');
				}
				$this->load->model('menu_model');
				$data['listmenu'] = $this->menu_model->showmenubyorder(2);
				$this->load->view('appstore/admin/dashboard',$data);
		}

	}


	public function edit_useradmin($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
					
				$this->load->model('admin_model');
				$this->load->model('menu_model');
					
				if (isset($_REQUEST['submit'])) {
					$level = $this->input->post('adminpage',true);
					$pass = $this->input->post('adminpass',true);
					$username = $this->input->post('adminname',true);
					$this->admin_model->edit_useradmin($id,md5($pass),$level);
					redirect('admincp/adminuser');
				}
					
				$data['listmenu'] = $this->menu_model->showmenubyorder(2);
				$data['adminuser'] = $this->admin_model->getDetailsAdmin($id);
				$this->load->view('appstore/admin/dashboard',$data);
		}

	}
	public function del_useradmin($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('admin_model');
				$this->admin_model->deladmin($id);
				redirect('admincp/adminuser');
		}
	}

	public function statuschange($id=null,$active=null){
		$this->load->model('catetype_model');
		$this->catetype_model->changestatustype($id,$active);
		redirect('admincp/type');
	}

	public function type(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
	
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('catetype_model');
				$data['alltype'] = $this->catetype_model->_showalltype();

				if (isset($_REQUEST['submit'])) {
					$nametype = $this->input->post('nametype',true);
					$this->catetype_model->add_type($nametype);
					redirect('admincp/type');
				}
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function add_type(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
	
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('catetype_model');
				$data['alltype'] = $this->catetype_model->_showalltype();

				if (isset($_REQUEST['submit'])) {
					$nametype = $this->input->post('nametype',true);
					$this->catetype_model->add_type($nametype);
					redirect('admincp/type');
				}
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}
	public function edit_type($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
	
				$this->load->model('catetype_model');
				if (isset($_REQUEST['submit'])) {
					$typeid = $id;
					$nametype = $this->input->post('nametype',true);
					$this->catetype_model->edit_type($typeid,$nametype);
					redirect('admincp/type');
				}
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$data['type'] = $this->catetype_model->_detailtype($id);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function del_type($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				$this->catetype_model->del_type($id);
				redirect('admincp/type');
		}
	}

	public function activecatetype($id=null,$active=null){

			$this->load->model('catetype_model');
			$this->catetype_model->changestatus($id,$active);
			redirect('admincp/categorytype');
	}
	public function categorytype(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				if (isset($_REQUEST['submit'])) {
					$catename = $this->input->post('namecategory',true);
					$typeid = $this->input->post('typecate',true);
					$cateroot = $this->input->post('cateroot',true);
					$active = $this->input->post('active',true);
					$this->catetype_model->add_categorytype($typeid,$catename,$cateroot,$active);
					redirect('admincp/categorytype');
				}
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$data['alltype'] = $this->catetype_model->_showalltype();
				//---------------------------------Paging
				$config['base_url'] = site_url('admincp/categorytype');
				$config['total_rows'] = $this->catetype_model->totalcate();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['allcate'] = $this->catetype_model->show_allcategory($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				//------------------------------End Paging
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function add_cate(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				if (isset($_REQUEST['submit'])) {
					$catename = $this->input->post('namecategory',true);
					$typeid = $this->input->post('typecate',true);
					$cateroot = $this->input->post('cateroot',true);
					$active = $this->input->post('active',true);
					$this->catetype_model->add_categorytype($typeid,$catename,$cateroot,$active);
					redirect('admincp/categorytype');
				}
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$data['alltype'] = $this->catetype_model->_showalltype();
				//---------------------------------Paging
				$config['base_url'] = site_url('admincp/categorytype');
				$config['total_rows'] = $this->catetype_model->totalcate();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['allcate'] = $this->catetype_model->show_allcategory($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				//------------------------------End Paging
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}
	public function del_catecontent($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				$this->catetype_model->del_category($id);
				redirect('admincp/categorytype');
		}
	}

	public function edit_catecontent($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				if (isset($_REQUEST['submit'])) {
					$catename = $this->input->post('namecategory',true);
					$typeid = $this->input->post('typecate',true);
					$cateroot = $this->input->post('cateroot',true);
					$active = $this->input->post('active',true);
					$this->catetype_model->edit_categorytype($id,$typeid,$catename,$cateroot,$active);
					redirect('admincp/categorytype');
				}
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$data['alltype'] = $this->catetype_model->_showalltype();
				$data['catedetails'] = $this->catetype_model->showdetailscate($id);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function add_cateapp(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('catetype_model');
				if (isset($_REQUEST['submit'])) {
					$catename = $this->input->post('namecategory',true);
					$typeid = $this->input->post('typecate',true);
					$cateroot = $this->input->post('cateroot',true);
					$active = $this->input->post('active',true);
					$this->catetype_model->add_categorytype($typeid,$catename,$cateroot,$active);
					redirect('admincp/categorytype');
				}
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('app_model');
				//---------------------------------Paging
				$config['base_url'] = site_url('admincp/categorytype');
				$config['total_rows'] = $this->app_model->totalcateapp();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
					
				$data['allcate'] = $this->app_model->show_allcategory($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				//------------------------------End Paging
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function status_member($id=null,$active=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{

				$this->load->model('user_model');
				$this->user_model->changestatustype($id,$active);
				redirect('admincp/usercp');
		}
	}


	public function active_app($id=null,$active=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('content_model');
				$this->content_model->changestatustype($id,$active);
				redirect('admincp/sitecontent');
			
		}
	}

	public function sitecontent(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('catetype_model');
				$this->load->model('content_model');
				$data['alltype'] = $this->catetype_model->_showalltype();
				$data['allcate'] = $this->catetype_model->showllcate();
				//---------------------------------Paging
				$config['base_url'] = site_url('admincp/sitecontent');
				$config['total_rows'] = $this->content_model->totalcontent();
				$config['per_page'] = 30;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['allcontents'] = $this->content_model->show_allcontent($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				//------------------------------End Paging
				$this->load->view('appstore/admin/dashboard',$data);
			
		}
	}

	public function del_sitecontent($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
		
				$this->load->model('content_model');
				if($id == null){
				}else{
					$this->content_model->del_content($id);
				}
				redirect('admincp/sitecontent');
		}
	}

	public function edit_sitecontent($id = null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
	
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('catetype_model');
				$this->load->model('content_model');
				$data['alltype'] = $this->catetype_model->_showalltype();
				//$data['allcate'] = $this->catetype_model->showllcate();

				//---------------------------------POST
				if (isset($_REQUEST['submit'])) {
					// 				echo mime_content_type('C:\\Documents and Settings\\Pham Xuan Son\\Desktop\\cavang.apk');
					// 				die();
					$typeid = $this->input->post('typeid', true);
					$cateid = $this->input->post('cateid', true);
					$coins = $this->input->post('coins',true);
					$namecontent = $this->input->post('namecontent', true);
					$description = $this->input->post('description', true);
					$active = $this->input->post('active', true);
					$mypath= "./src/imagescontent";
					$image=  $this->do_upload_image($mypath,'images');
					$ifiles2=  $this->do_upload_image($mypathfile,'ifiles_2');
					$ifiles3=  $this->do_upload_image($mypathfile,'ifiles_3');
					$mypathfile= "./apps";
					$ifiles=  $this->do_upload_file($mypathfile,'ifiles');
						
					$this->content_model->update_content($id,$cateid,$typeid,$namecontent,$description,$image,$coins,$ifiles,$ifiles2,$ifiles3,$active);
					redirect('admincp/sitecontent');
				}
				//------------------------------End POST
				$data['contents'] = $this->content_model->show_detailscontent($id);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}
	public function add_sitecontent(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('catetype_model');
				$this->load->model('content_model');
				$data['alltype'] = $this->catetype_model->_showalltype();
				//$data['allcate'] = $this->catetype_model->showllcate();

				//---------------------------------POST
				if (isset($_REQUEST['submit'])) {
					// 				echo mime_content_type('C:\\Documents and Settings\\Pham Xuan Son\\Desktop\\cavang.apk');
					// 				die();
					$typeid = $this->input->post('typeid', true);
					$cateid = $this->input->post('cateid', true);
					$coins = $this->input->post('coins',true);
					$namecontent = $this->input->post('namecontent', true);
					$description = $this->input->post('description', true);
					$active = $this->input->post('active', true);
					$mypath= "./src/imagescontent";
					if ( !$image=  $this->do_upload_image($mypath,'images')){
						$error = array( 'error' => $this->upload->display_errors() );
						var_dump($error);die();
					}
					$ifiles2=  $this->do_upload_image($mypath,'ifiles_2');
					$ifiles3= $this->do_upload_image($mypath,'ifiles_3');
					$mypathfile= "./apps";
				
					if ( ! $ifiles=  $this->do_upload_file($mypathfile,'ifiles') ) {
						$error = array( 'error' => $this->upload->display_errors() );
						// exit();
						// $this->load->view( 'upload_form', $error );
						var_dump($error);exit();
							
					}
						
					$this->content_model->add_newcontent($cateid,$typeid,$namecontent,$description,$image,$coins,$ifiles,$ifiles2,$ifiles3,$active,$this->session->userdata('adminid'));
					redirect('admincp/sitecontent');
				}
				//------------------------------End POST
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}
	function ajaxtypetocate($id){

		$this->load->model('catetype_model');
		$catebytype = $this->catetype_model->_show_cateogry_by_type($id);

		echo  "<select name='cateid'>";
		if($catebytype <> null){
			foreach($catebytype as $row){
				echo  '<option value='.$row->id.'>'.$row->catename.'</option>';
			}
		}else{
			echo  '<option value=0 >-----------------</option>';
		}
		echo  '</select>';

	}
	public function notification(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------Addition Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				//$data['reportbydays'] = $this->report_model->get_report_day();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------Addition Function ----------------------------------
					
				if (isset($_REQUEST['notifysubmit'])) {
					$title = $this->input->post('tieude', true);
					$content = $this->input->post('noidung', true);
					if(strlen($title) <= 0 && strlen($content) <=0){
						redirect('admincp/notification');
					}else{
						$this->load->model('post_model');
						$this->post_model->add_notify($title,$content);
						redirect('admincp/notification');
					}
				}
				$data['shoutbox'] =0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->model('post_model');
				$config['base_url'] = site_url('admincp/notification');
				$config['total_rows'] = $this->post_model->totalnotify();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu tiên';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['receivesmo'] = $this->post_model->show_notify($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function delnotify($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('post_model');
				if($id==null){
				}else{
					$this->post_model->delnotify($id);
				}
				redirect('admincp/temp2/notification');
		}
	}

	public function blockip(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				if (isset($_REQUEST['ipsubmit'])) {
					$ipaddress = $this->input->post('ipaddress', true);
					$this->load->model('log_model');
					$this->log_model->add_ipaddress($ipaddress);
					redirect('admincp/blockip');
				}
				$data['shoutbox'] =0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->model('log_model');
				$config['base_url'] = site_url('admincp/blockip');
				$config['total_rows'] = $this->log_model->_totalrows();
				$config['per_page'] = 30;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['ipblocks'] = $this->log_model->listpageipaddress($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function add_ip(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				if (isset($_REQUEST['ipsubmit'])) {
					$ipaddress = $this->input->post('ipaddress', true);
					$this->load->model('log_model');
					$this->log_model->add_ipaddress($ipaddress);
					redirect('admincp/blockip');
				}
				$data['shoutbox'] =0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->model('log_model');
				$config['base_url'] = site_url('admincp/blockip');
				$config['total_rows'] = $this->log_model->_totalrows();
				$config['per_page'] = 30;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['ipblocks'] = $this->log_model->listpageipaddress($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function deleteip($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('log_model');
				if(is_numeric($id)){
					$this->log_model->deleteip($id);
					redirect('admincp/blockip');
				}else{
					redirect('admincp/blockip');
				}
		}
	}
	public function login($err=null){

		if (isset($_REQUEST['submit'])) {
			$username = $this->input->post('uname', true);
			$pass1 = $this->input->post('pass1', true);
			$this->load->model('admin_model');
			$result = $this->admin_model->checkuser($username,md5($pass1));

			if($result == null){
				redirect('admincp/login/'.rand(1,10));//lá»—i sai username hoáº·c pass
			}else {
				$newdata = array(
						'adminid'  => $result,
						'adminnam'     => $username
				);
				$this->session->set_userdata($newdata);
				redirect('admincp/index');
			}
		}
		if($err <> null){
			$err = "<font color=red>Error Code: " . $err .'</font>';
		}
		$data['caution'] = $err;
		$data['shoutbox'] =0;
		$this->load->view('appstore/admin/index',$data);
	}

	public function usercp(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('user_model');
				$this->load->model('usertype_model');
				
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$data['shoutbox'] =0;
				$config['base_url'] = site_url('admincp/usercp');
				$config['total_rows'] = $this->user_model->totaluser();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['listusers'] = $this->user_model->_listuser($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['listusertype'] = $this->usertype_model->get_all();
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function pushmail($from,$to,$subject,$message){
		$config = Array(
				'protocol' => 'smtp',
				'smtp_host' => 'ssl://smtp.googlemail.com',
				'smtp_port' => 465,
				'smtp_user' => 'sonpx@store8x.com',
				'smtp_pass' => 'Xuanson123!',
				'mailtype'  => 'html',
				'charset'   => 'utf-8'
		);
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");
		$this->email->from($from, 'M8X-Application Mobile Content-Store8x.com');
		$this->email->to($to);

		$this->email->subject($subject);
		$this->email->message($message);
		// Set to, from, message, etc.

		$result = $this->email->send();
		echo $result;
	}


	public function listapp(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$data['shoutbox'] =0;
				$this->load->model('app_model');
				$config['base_url'] = site_url('admincp/listapp');
				$config['total_rows'] = $this->app_model->_totalapp();
				$config['per_page'] = 10;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['listapps'] = $this->app_model->listapp($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$data['totalapp'] = $this->app_model->_totalapp();
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function delapp($id= null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('app_model');
				if($id <> null){
					$this->app_model->delapp($id);
					redirect('admincp/listapp');
				}else{
					redirect('admincp/listapp');
				}
		}
	}

	public function add_app(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				if (isset($_REQUEST['submit'])) {
					$name = $this->input->post('appname', true);
					$appdes = $this->input->post('appdes', true);
					$apptype = $this->input->post('apptype', true);
					//$appicon = $this->input->post('appicon', true);
					$applink = $this->input->post('applink', true);
					$appfile = $this->input->post('appfile', true);
					$mypath= "./src/app";
					$image=  $this->do_upload_image($mypath,'appicon');
					if($image == null || strlen($name) <= 0){
						$data['shoutbox'] = 3;
					}else{
						$data['shoutbox'] = 1;
						$this->load->model('app_model');
						$this->app_model->add_app($name,$appdes,$apptype,$image,$applink);
						redirect('admincp/listapp');
					}

				}
				$data['shoutbox'] = 0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function needhave($id=null,$needhave){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model("content_model");
				$this->content_model->changestatusneedhave($id,$needhave);
				redirect("admincp/sitecontent");
			
		}
	}
	public function content(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------Addition Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				//$data['reportbydays'] = $this->report_model->get_report_day();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------Addition Function ----------------------------------

				$this->load->model('post_model');
				$config['base_url'] = site_url('admincp/content');
				$config['total_rows'] = $this->post_model->totalarticle();
				$config['per_page'] = 5;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['listcontent'] = $this->post_model->show_article($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$data['totalcontent'] = $this->post_model->totalarticle();
				if (isset($_REQUEST['submit'])) {
					$tieude = $this->input->post('tieude', true);
					$noidung = $this->input->post('noidung',true);
					$active = $this->input->post('active',true);
					$menuid= $this->input->post('categorycontent',true);
					$this->post_model->add_article($menuid,$this->session->userdata('adminid'),$tieude,$noidung,$active);
					redirect('admincp/content');
				}
				$this->load->model('menu_model');
				$data['rootmenu'] = $this->menu_model->showmenubyorder(2);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function add_news(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				//--------------Addition Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				//$data['reportbydays'] = $this->report_model->get_report_day();
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------Addition Function ----------------------------------

				$this->load->model('post_model');
				$config['base_url'] = site_url('admincp/content');
				$config['total_rows'] = $this->post_model->totalarticle();
				$config['per_page'] = 5;
				$config['prev_link'] = 'Đầu';
				$config['next_link'] = 'Tiếp';
				$this->pagination->initialize($config);
				$data['listcontent'] = $this->post_model->show_article($config['per_page'],$this->uri->segment(3));
				$data['pages'] = $this->pagination->create_links();
				$data['totalcontent'] = $this->post_model->totalarticle();
				if (isset($_REQUEST['submit'])) {
					$tieude = $this->input->post('tieude', true);
					$noidung = $this->input->post('noidung',true);
					$active = $this->input->post('active',true);
					$menuid= $this->input->post('categorycontent',true);
					$this->post_model->add_article($menuid,$this->session->userdata('adminid'),$tieude,$noidung,$active);
					redirect('admincp/content');
				}
				$this->load->model('menu_model');
				$data['rootmenu'] = $this->menu_model->showmenubyorder(2);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function edit_news($id=null){

		if($this->session->userdata('adminnam')==null){
			redirect('admincp/login');
		}else{
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$this->load->model('post_model');
				$data['content_detail'] = $this->post_model->showdetailpost($id);
				if (isset($_REQUEST['submit'])) {
					$tieude = $this->input->post('tieude', true);
					$noidung = $this->input->post('noidung',true);
					$active = $this->input->post('active',true);
					$menuid= $this->input->post('categorycontent',true);
					$this->post_model->edit_post($id,$menuid,$this->session->userdata('adminid'),$tieude,$noidung,$active);
					redirect('admincp/content');
				}
				$this->load->model('menu_model');
				$data['rootmenu'] = $this->menu_model->showmenubyorder(2);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function searchtotalbymonth(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{			
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
				$thang = null;
				$nam=null;
				if (isset($_REQUEST['submit'])) {
					$thang = $this->input->post("thangdate",true);
					$nam = $this->input->post("namdate",true);
					$data['values'] = $this->transms_model->totalmobymonth($thang,$nam);
				}
				$data['values'] = $this->transms_model->totalmobymonth($thang,$nam);
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}


	function totalinmonth($dulieu=null){
		$this->load->model("transms_model");
		$dulieuraw= explode("%20",$dulieu);
		$values= $this->transms_model->totalmobymonth($dulieuraw[0],$dulieuraw[1]);
		echo  "<h3>";
		echo $values." sms = <font color='blue'>". number_format($values*5200). "</font> vnd";
		echo  '</h3>';

	}


	public function delcontent($id=null){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('post_model');
				$this->post_model->delcontent($id);
				redirect('admincp/content');
		}
	}
	public function editapp(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->view('admincp/temp2/index',$data);
		}
	}
	//--------------------------Quan ly file
	public function fileservices(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$data['shoutbox'] = 0;
				$this->load->model('service_model');
				if (isset($_REQUEST['submitservices'])) {
					$name = $this->input->post('name', true);
					$servicesid = $this->input->post('servicesid',true);
					$return_value = $this->input->post('return_value',true);
					$mypath= "./src/file";
					$mtfile=  $this->do_upload_file($mypath,'mtfile');
					if($mtfile == null ){
						$data['shoutbox'] = 3;
					}else{
						$data['shoutbox'] = 1;
						$this->service_model->add_mtresult($name,$servicesid,$return_value,$mtfile);
						redirect('admincp/fileservices');
					}
				}

				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$data['mts'] = $this->service_model->show_mtresult();
				$data['services'] = $this->service_model->show_services();
				$this->load->view('admincp/temp2/index',$data);
		}
	}
	//--------------------------Quan ly services
	public function servicescontrol(){
			
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('service_model');
				//--------------BEGIN Function ----------------------------------
				$this->load->model("transms_model");
				$this->load->model('report_model');
				$this->load->model('chatbox_model');
				$this->load->model('user_model');
				$this->load->model('transms_model');
				$data['totaluser'] = $this->user_model->totaluser();
				$data['smsday'] = $this->transms_model->totalmotoday(date('d-m-Y'));
				$data['totalsms'] = $this->transms_model->totalmo();
				//--------------END Function ----------------------------------
					
				if (isset($_REQUEST['submitservices'])) {
					$shortcode = $this->input->post('shortcode', true);
					$prefix_1 = $this->input->post('prefix_1',true);
					$prefix_2 = $this->input->post('prefix_2', true);
					$partner = $this->input->post('partner',true);
					$apptype = $this->input->post('typeapp',true);
					$return_value = $this->input->post('return_value', true);
					$this->service_model->add_services($shortcode,$prefix_1,$prefix_2,$return_value,$partner,$apptype);
					redirect('admincp/servicescontrol');
				}

				$data['services']  = $this->service_model->show_services();
				$data['shoutbox'] = 0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->view('appstore/admin/dashboard',$data);
		}
	}

	public function unactiveservice($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('service_model');
				$this->service_model->unactive($id);
				redirect('admincp/servicescontrol');
		}
	}
	public function activeservice($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			
				$this->load->model('service_model');
				$this->service_model->activesms($id);
				redirect('admincp/servicescontrol');
			
		}
	}
	public function delservices($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$this->load->model('service_model');
				$this->service_model->delservices($id);
				redirect('admincp/servicescontrol');
		}
	}

	public function servicescost(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$data['shoutbox'] =0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->view('admincp/temp2/index',$data);
			
		}
	}
	public function withdraw(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
				$data['shoutbox'] =0;
				$data['adminname'] = $this->session->userdata('adminnam');
				$data['adminid'] = $this->session->userdata('adminid');
				$this->load->view('admincp/temp2/index',$data);
		}
	}
	function do_upload_image($mypath,$filename){
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'gif|jpg|png|bmp';
		$config['max_size']	= '80000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}

	function do_upload_file($mypath,$filename){
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'jar|jad|apk|ipa';
		$config['max_size']	= '500000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(isset($filename))
		{
			//echo $filename; die();

			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				//print_r($error);
				return null;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}


	public function logout(){
		$this->session->unset_userdata('adminid');
		$this->session->unset_userdata('adminnam');
		$this->session->sess_destroy();
		redirect('admincp');
	}

}
